<template>
  <div class="numberplatePay">
    <c-title :hide="false" :text="'车牌付'"></c-title>
    <div class="list">
      <div class="lis" v-for="(item, index) in infoList" :key="index">
        <div class="top">
          <div class="plate">{{ item.license_no }}</div>
          <div class="tag" v-if="item.is_special">{{ item.group_name }}</div>
        </div>
        <div class="bottom">
          <div class="bottom_l">开通车牌付</div>
          <div class="bottom_r">
            <van-switch v-model="item.license_pay" size="24px" active-color="#F15353" @change="onChange(item.id, index)" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      infoList: [],

      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  activated() {
    this.initGet();
    this.getData();
  },
  methods: {
    async onChange(id, index) {
      let json = { car_id: id };
      this.post_Method(json, index);
    },
    async post_Method(json, index) {
      let { msg, result } = await $http.post("plugin.car-manage.frontend.controllers.car.license-pay", json, "");
      if (result) {
        this.$set(this.infoList[index].license_pay, "", !this.infoList[index].license_pay);
        this.$toast(msg);
      } else {
        this.$toast(msg);
      }
    },
    initGet() {
      this.page = 1;
      this.total_page = 0;
      this.isLoadMore = true;
      this.infoList = [];
    },
    async getData() {
      let { data, msg, result } = await $http.post("plugin.car-manage.frontend.controllers.car.index", {}, "");
      if (result) {
        this.infoList = data.data;
        this.total_page = data.last_page;
        if (!this.total_page) {
          this.total_page = 0;
        }
        this.isLoadMore = true;
      } else {
        this.$toast(msg);
      }
    },
    async getMoreData() {
      if (!this.isLoadMore) return;
      this.isLoadMore = false;
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page += 1;
        let { data, msg, result } = await $http.post("plugin.car-manage.frontend.controllers.car.index", { page: this.page }, "");
        this.isLoadMore = true;
        if (result) {
          this.infoList = this.infoList.concat(data.data);
        } else {
          app.$toast(msg);
          this.isLoadMore = false;
          this.page = this.page - 1;
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.list {
  .lis {
    margin: 0.63rem 0.75rem;
    margin-bottom: 0;
    background: #fff;
    padding: 0.84rem 0.75rem;
    border-radius: 0.5rem;
    .top {
      display: flex;
      align-items: center;
      .plate {
        font-size: 1.31rem;
        font-weight: bold;
      }
      .tag {
        padding: 0.19rem 0.31rem;
        font-size: 0.75rem;
        color: #f15353;
        background: #feeded;
        border-radius: 0.25rem;
        box-sizing: border-box;
        margin-left: 0.25rem;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }

    .bottom {
      display: flex;
      justify-content: space-between;
      margin-top: 1.88rem;
    }
  }
}
</style>
